#include "reg52.h"

sfr AUXR = 0x8e;
sbit S5 = P3^2;
sbit S4 = P3^3;

unsigned char code SMG[18]={0xc0,0xf9,
	  0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
    0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};

unsigned int count;
unsigned char stat_LED = 0xff;
unsigned char s;
unsigned char m;
unsigned char h;	
unsigned char command;		

//选择锁存器和初始化系统------------------------
void SelectHC573(unsigned char channel)
{
	switch(channel)
	{
		case 4:
			P2 = (P2 & 0x1f) | 0x80;
			break;
		case 5:
			P2 = (P2 & 0x1f) | 0xa0;
			break;
		case 6:
			P2 = (P2 & 0x1f) | 0xc0;
			break;
		case 7:
			P2 = (P2 & 0x1f) | 0xe0;
			break;
		case 0:
			P2 = (P2 & 0x1f) | 0x00;
			break;		
	}
}

void InitSystem()
{
	SelectHC573(5);
	P0 = 0x00;
	SelectHC573(4);
	P0 = 0xff;
	SelectHC573(0);
}

//检查灯和数码管----------------------------------
void Delay(unsigned int t)
{
	while(t--);
}


void CheckLED()
{
	unsigned char i = 0;
	SelectHC573(4);
	for (i = 1;i <= 8;i++)
	{
		stat_LED = 0xfe <<i;
		P0 = stat_LED;
		Delay(60000);
	}
	
	for (i = 1;i <= 8;i++)
	{
		stat_LED = ~(0xfe <<i);
		P0 = stat_LED;
		Delay(60000);
	}
	SelectHC573(0);
}

void CheckSMG()
{
	unsigned char i = 0;
	for (i = 1;i <= 8;i++)
	{
		SelectHC573(7);
		P0 = 0x00;
		SelectHC573(6);
		P0 = ~(0xfe << i);
		Delay(60000);
	}
	
	for (i = 1;i <= 8;i++)
	{
		SelectHC573(7);
		P0 = 0x00;
		SelectHC573(6);
		P0 = 0xfe << i;
		Delay(60000);		
	}
	SelectHC573(0);
}
//定时器与串口配置------------------------------
void InitT0()
{
	TMOD = 0x21;
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	
	EA = 1;
	TR0 = 1;
	ET0 = 1;
}

void ServeT0()		interrupt 1
{
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	count++;
	if(count == 20)
	{
		s++;
		count = 0;
	}
	
	if(s == 60)
	{
		m++;
		s = 0;
	}
	
	if(m == 60)
	{
		h++;
		m = 0;
	}
}

void InitUart()
{
	TMOD = 0x21;
	TH1 = 0xfd;
	TL1 = 0xfd;
	
	AUXR = 0x00;
	SCON = 0x50;
	
	EA = 1;
	ES = 1;
	TR1 = 1;
}	

void ServeUart() interrupt 4
{
	TH1 = 0xfd;
	TL1 = 0xfd;
	if(RI == 1)
	{
		RI = 0;
		command = SBUF;
	}	
}

void Sending(unsigned char dat)
{
	SBUF = dat;
	while(TI == 0);
	TI = 0;
}

//数码管运行时间-------------

void ShowSMGOnly(unsigned char value,unsigned char location)
{
	P0 = 0xff;
	SelectHC573(7);
	P0 = value;
	SelectHC573(6);
	P0 = 0x01 << location;
	SelectHC573(0);
}

void DisplayTime()
{
	ShowSMGOnly(SMG[s%10],7);
	Delay(500);
	ShowSMGOnly(SMG[s/10],6);
	Delay(500);
	ShowSMGOnly(SMG[16],5);
	Delay(500);	
	
	ShowSMGOnly(SMG[m%10],4);
	Delay(500);
	ShowSMGOnly(SMG[m/10],3);
	Delay(500);
	ShowSMGOnly(SMG[16],2);
	Delay(500);
	
	ShowSMGOnly(SMG[h%10],1);
	Delay(500);
	ShowSMGOnly(SMG[h%10],0);
	Delay(500);	
}

//控制LED-----------
void DelaySMG(unsigned int t)
{
	while(t--)
	{
		DisplayTime();
	}
}

void ScanKey()
{
	if(S5 == 0)
	{
		DelaySMG(100);
		if(S5 == 0)
		{
			while(S5 == 0)
			{
			DisplayTime();
			}
			SelectHC573(4);
			stat_LED = (stat_LED & 0xbf) | (~stat_LED & 0x40);
			P0 = stat_LED;
			SelectHC573(0);
		}
	}
	
	if(S4 == 0)
	{
		DelaySMG(100);
		if(S4 == 0)
		{
			while(S4 == 0)
			{
				DisplayTime();
			}
			SelectHC573(4);
			stat_LED = (stat_LED & 0x7f) | (~stat_LED & 0x80);
			P0 = stat_LED;
			SelectHC573(0);
		}
	}
}

void ControlLED()
{
	if(command != 0x00)
	{
		SelectHC573(4);
		switch(command & 0xf0)
		{
			case 0xa0:
				stat_LED = (stat_LED | 0x0f) & (~command | 0xf0);
				P0 = stat_LED;
				SelectHC573(0);
				command = 0x00;			
				break;
			case 0xb0:
				Sending(h/10 <<4 | h%10);
				Sending(m/10 <<4 | m%10);
				Sending(s/10 <<4 | s%10);
				SelectHC573(0);
				command = 0x00;
				break;
		}
	}
}
//主函数-------------
void main()
{
	InitSystem();
	CheckLED();
	CheckSMG();
	InitT0();
	InitUart();

	while(1)
	{
		DisplayTime();
		ScanKey();
		ControlLED();
	}	
	
}